﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using System.Text;
using System.Configuration;

public partial class admin_infomation_ExplainManger : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string data = Request.Form["data"];
            int langid=int.Parse(Request.Form["langid"]);
            string fnType=Request.Form["fntype"];
            int id = 0;
            string keyname=Request.Form["keyname"];
            int currpage = 1;
            if (Request.Form["currpage"]!=null)
            {
                currpage = int.Parse(Request.Form["currpage"]);
            }
                
            int sumpage=1;
            if (Request.Form["sumpage"]!=null)
            {
                sumpage = int.Parse(Request.Form["sumpage"]);
            }
            
            
            if (Request.Form["id"]!=null)
            {
                id = int.Parse(Request.Form["id"].ToString());
            }
            
            switch (fnType)
            {
                case "toedit"
                : toEdit(id,langid);
                    break;
                case "getall"
                : Filllist(langid);
                    break;
                    case "find"
                : find( langid,keyname);
                    break;
                    case "showsum"
                : showsum(sumpage,langid);
                    break;
                    case "pagelist"
                : pagelist(currpage,sumpage,langid);
                    break;
                case "del"
                : del(id, langid);
                    break;
                default:
                    break;
            }
          
        }
    }
    public void  Filllist(int langid)
    {
        AudioBLL ab = new AudioBLL(langid);
        List<Audio> la = ab.getALL();
        Session["pagelist"] = la;
        FillHTML(la);
    }
    public void find(int langid,string keyname)
    {
        AudioBLL ab = new AudioBLL(langid);
        List<Audio> la = ab.find(keyname);
        FillHTML(la);
    }
    private void FillHTML(List<Audio> la)
    {
        StringBuilder stb = new StringBuilder();
        int a = 0;
        foreach (Audio au in la)
        {
           
            a++;
            stb.Append("<div   style='height:170px;width:120px;border-style:outset;float:left;margin-left:10px'>");
            stb.AppendFormat("<a href='#' onclick='del({0})' style='background-color:Black;color:White;float:right'>╳</a>", au.Id);
            //stb.Append("<center>");
            stb.AppendFormat("<img src='{0}' class='{3}' name='{2}' alt='{1}' style='height:110px;width:90px'/>", (ConfigurationManager.AppSettings["AudFilePath"] + au.Bigimg).Replace("\\", "/"), au.Description, au.Name, au.Author);
            stb.AppendFormat("<br><span style='font-size:14px'>名称：{0}</span>",au.Name);
            stb.AppendFormat("<br><span style='font-size:14px'>作者：{0}</span>", au.Author);
            stb.AppendFormat("<br><input type='button'  onclick='toedit({0})' value='修改'/>", au.Id, au.Order);
            //stb.Append("</center>");
            stb.Append("</div>");
            if (a % 3 == 0)
            {
                stb.Append("<p/>");

            }



        }
        string str = stb.ToString();
        StringBuilder stbeveal = new StringBuilder();
        //stbeveal.AppendFormat(" document.getElementById('AudioList').innerHTML ='{0}';",str.ToString());
        Response.Write(str.Substring(0, str.Length - 1));
        //Response.Write(stbeveal.ToString());
    }
    public  void add(Audio aud,int langid)
    {

        AudioBLL ab = new AudioBLL(langid);
         Response.Write(  ab.add(aud)?"1":"0");
    

    }
    public void update(Audio aud,int langid)
    {
        AudioBLL ab = new AudioBLL(langid);
        Response.Write( ab.update(aud)?"1":"0");
    }
    public void del(int id,int langid)
    {
        AudioBLL ab = new AudioBLL(langid);
        Response.Write(ab.del(id) ? "alert('删除成功');showsum()" : "alert('删除失败')");
    }
    public Audio toMod(string  audtxt)
    {
        string[] audtxtArr = audtxt.Split('|');
        Audio aud = new Audio(int.Parse(audtxtArr[0]));
        aud.Name = audtxtArr[1];
        aud.Author = audtxtArr[2];
        aud.Description = audtxtArr[3];
        aud.Release = Convert.ToBoolean( audtxtArr[4]);

        return aud;
    }
    public void toEdit(int id,int langid)
    {
        AudioBLL adbll = new AudioBLL(langid);
        Audio aud= adbll.toEdito(id);
        StringBuilder stb = new StringBuilder();
        stb.Append(" editshow();");
        stb.AppendFormat("$('#AudName').val('{0}');",aud.Name);
        stb.AppendFormat("$('#AudAuthor').val('{0}');", aud.Author);
        stb.AppendFormat("$('#AudOrder').val('{0}');",aud.Order);
        stb.AppendFormat("$('#Audp').val('{0}');", aud.Description);
        stb.AppendFormat("$('#edid').val('{0}');",aud.Id);
        stb.AppendFormat("$('#ReleasID').selected=={0};", aud.Release);
        Response.Write(stb.ToString());

    }
    public void showsum(int sumpage,int langid)
    {

        Response.Write(pagecurr(new AudioBLL(langid).getALL(),sumpage));
    }
   private string pagecurr(List<Audio> la,int sumpage)
    {
        StringBuilder stb = new StringBuilder();
        if (sumpage < la.Count)
        {
            int pas = la.Count / sumpage;
            for (int i = 1; i < pas+1; i++)
            {
                stb.AppendFormat("&nbsp<a href='#' onclick='pagelist({0},{1})'>{0}</a>", i.ToString(),sumpage.ToString());

            }
            if (la.Count%sumpage>0)
            {
               stb.AppendFormat("&nbsp<a href='#' onclick='pagelist({0},{1})'>{0}</a>",(pas+1).ToString(),sumpage.ToString());
            }

            return stb.ToString();


        }
        else
        {
            stb.AppendFormat("&nbsp<a href='#' onclick='pagelist({0},{1})'>{0}</a>","1",la.Count.ToString());
            return stb.ToString();
        }

    }
   private void pagelist(int currpage,int sumpage,int langid)
   {
       List<Audio> la = new List<Audio>();
       
           AudioBLL ab = new AudioBLL(langid);
           la = ab.getALL();
          
           

      
       List<Audio> lanew = new List<Audio>();
       for (int i = (currpage - 1) * sumpage; i < ((currpage * sumpage) > la.Count ? la.Count : currpage * sumpage); i++)
       {
           lanew.Add(la[i]);
       }
       FillHTML(lanew);
     

   }



    
}